package com.example.market.controller;

import com.example.market.entity.Goods;
import com.example.market.entity.Main;
import com.example.market.entity.Supplier;
import com.example.market.repository.SupplierRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/supplier")
public class SupplierHandler {
    @Autowired
    private SupplierRepository supplierRepository;

    @GetMapping("/findAll/{page}/{size}")
    public Page<Supplier> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        return supplierRepository.findAll(pageable);
    }

    @PostMapping("/save")
    public String save(@RequestBody Supplier supplier){
        Supplier result = supplierRepository.save(supplier);
        if(result != null){
            return "success";
        }else{
            return "error";
        }
    }

    @GetMapping("/findById/{id}")
    public Supplier findById(@PathVariable("id") Integer sno){
        return supplierRepository.findById(sno).get();
    }

    @PutMapping("/update")
    public String update(@RequestBody Supplier supplier ){
        Supplier result = supplierRepository.save(supplier);
        if(result != null){
            return "success";
        }else{
            return "error";
        }
    }

    @DeleteMapping("deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer sno){
        supplierRepository.deleteById(sno);
    }



}
